<!-- 首页 -->
<style lang="scss">
body {
  background-color: $light-gray-color;
}

.home-nav {
  width: 10rem;
  height: 2.3467rem;
  margin-top: 1.1rem;
  background: $gradient-color;


  .home-nav-top {
    width: 10rem;
    height: 1.0667rem;
    @include flex-space-between-center;


    .icon-dingwei {
      font-size: .64rem;
      color: $white-color;
    }

    h3 {
      font-size: .5867rem;
      font-weight: 500;
      color: $white-color;
    }

    .input {
      width: 5.3333rem;
      height: .8rem;
      background-color: $white-color;
      @include flex-center;
      position: relative;
      border-radius: .1rem;

      input {
        width: 4.5333rem;
        height: .8rem;
        border: none;
        outline: none;
        font-size: .4rem;
        color: #999;
        padding-left: .4rem;
        border-radius: .1rem;
      }

      .icon-sousuo {
        font-size: .4rem;
        color: #999;
        position: absolute;
        right: .4rem;
      }
    }

    .icon-dianpu {
      font-size: .7467rem;
      color: $white-color;
    }
  }

  .home-nav-bottom {
    width: 10rem;
    height: 1.1733rem;
    margin-top: .1rem;
    display: flex;
    align-items: center;
    position: relative;

    ul {
      width: 8.4rem;
      @include horizontal-scroll-hidden;

      /* Firefox */
      li {
        font-size: $lifontsize;
        flex-shrink: 0;
        /* 防止项目收缩 */
        padding: 0 .4rem;
        /* 添加一些内边距 */
        color: $white-color;
      }
    }

    .home-nav-bottom-right {
      width: 1.6rem;
      height: 1.1733rem;
      font-size: $lifontsize;
      color: $white-color;
      position: absolute;
      top: 0;
      right: 0;
      @include flex-center;

    }

  }
}

.home-banner {
  width: 10rem;
  height: 5.3333rem;
  margin-top: 0.1rem;
  background-color: #FF6040;

  .banner {
    width: 10rem;
    height: 5.3333rem;
    display: flex;
    overflow: hidden;
    position: relative;

    ul {
      display: flex;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 2;
      transition: all 1s;
    }

    img {
      width: 10rem;
      height: 5.3333rem;
    }

    .banner-dots {
      width: 10rem;
      height: 0.2rem;
      position: absolute;
      bottom: 0.2rem;
      @include flex-center;
      z-index: 3;
    }

    .dot {
      width: 0.2rem;
      height: 0.2rem;
      background-color: $white-color;
      border-radius: 50%;
      margin: 0 0.1rem;
    }

    .dot-active {
      background-color: black;
    }
  }
}

.quicknav {
  width: 10rem;
  height: 2.3467rem;
  margin-top: 0.1rem;
  background-color: $white-color;
  margin-top: 0.3rem;
  margin-bottom: 0.3rem;

  ul {
    @include flex-space-around-center;

    li {
      width: 2.08rem;
      height: 2.3467rem;
      @include flex-center;
      flex-direction: column;
      font-size: 0.3rem;
      color: $black-color;

      img {
        width: .8533rem;
        height: .8533rem;
      }
    }
  }
}

.seckill {
  width: 10rem;
  height: 4.6667rem;
  // background-color: $white-color;
  display: flex;

  .seckill-left {
    width: 4.6667rem;
    height: 4.6667rem;
    @include flex-center;

    img {
      width: 4.6667rem;
      height: 4.6667rem;
    }
  }

  .seckill-right {
    width: 5.3333rem;
    height: 4.6667rem;

    img {
      width: 5.3333rem;
      height: 2.2667rem;
    }
  }
}

.tuijian {
  width: 10rem;
  height: 4rem;
  background-color: $white-color;
  margin-top: 0.3rem;
  margin-bottom: 0.3rem;

  .tuijian-top {
    width: 10rem;
    height: 1.1467rem;
    // background-color: pink;
    @include flex-space-around-center;

    h4 {
      font-size: 0.4rem;
      font-weight: 400;
      // color: $black-color;
      display: inline-block;
    }
  }

  .tuijian-bottom {
    width: 10rem;
    height: 3.1rem;
    // background-color: pink;
    @include flex-space-around-center;

    ul {
      width: 10rem;
      height: 3.1rem;
      @include horizontal-scroll-hidden;

      li {
        width: 2.5rem;
        height: 3.1rem;
        display: flex;
        flex-shrink: 0;
        /* 防止项目收缩 */

        img {
          width: 2.1333rem;
          height: 2.1867rem;
        }
      }
    }
  }
}

.active {
  color: $orange-color;
}

.hort {
  width: 10rem;
  background-color: #fff;

  .hort-top {
    width: 10rem;
    height: .7733rem;
    font-size: $lifontsize;
    color: $white-color;

    ul {
      @include flex-space-around-center;

      li {
        color: $orange-color;
        // background-color: orange;
        width: 3.3333rem;
        height: .7733rem;
        @include flex-center;
      }

      .hort-top-li-active {
        color: $white-color;
        background-color: $orange-color;
      }
    }
  }
}

.hort-bottom {
  width: 10rem;

  .hort-box {
    width: 10rem;
    height: 2.6667rem;
    background-color: $white-color;
    display: flex;
    align-items: center;
    padding: 0.21333rem 0.42667rem;

    .hort-box-left {
      width: 2.24rem;
      height: 2.24rem;
      @include flex-center;
      flex-direction: column;
      position: relative;

      img {
        width: 2.24rem;
        height: 2.24rem;
      }

      p {
        font-size: 0.24rem;
        position: absolute;
        left: 0;
        top: .1333rem;
        display: inline-block;
        background-color: $orange-color;
        color: $white-color;
        padding: 0.05333rem 0.13333rem;
        border-radius: 0 0.8rem 0.8rem 0;
      }
    }

    .hort-box-right {
      width: 6.6133rem;
      height: 2.24rem;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .hort-box-right-top {
        p {
          font-size: .32rem;
        }
      }

      .hort-box-right-bottom {
        display: flex;
        justify-content: space-between;
        align-items: center;

        p {
          font-size: .3733rem;

          i {
            font-size: .32rem;
            text-decoration: line-through;
          }
        }



        button {
          background-color: $orange-color;
          color: $white-color;
          border: 0;
          width: 1.7333rem;
          height: .86rem;
          border-radius: .08rem;
        }
      }
    }
  }
}
</style>
<template>
  <div class="home">
    <div class="home-nav">
      <div class="home-nav-top">
        <i class="iconfont icon-dingwei"></i>
        <h3 @click="getHome">严选商城</h3>
        <div class="input">
          <input type="text" placeholder="请输入搜索内容"></input>
          <i class="iconfont icon-sousuo"></i>
        </div>
        <i class="iconfont icon-dianpu"></i>
      </div>
      <div class="home-nav-bottom">
        <ul>
          <li v-for="item in navList" :key="item.id" @click="getGoodsList(item.id)">
            {{ item.catename }}
          </li>
        </ul>
        <div class="home-nav-bottom-right">
          <i class="iconfont icon-fenlei"></i>
          分类
        </div>
      </div>
    </div>
    <!-- 首页banner -->
    <div class="home-banner">
      <div class="banner" ref="banner" @touchend.stop="handleTouchEnd">
        <ul ref="banner">

          <li v-for="item, index in bannerLiul ref =" banner">
          <li :key="iem.id">
            <img : rc=".1000/.id'>
  item.img" alt="">
            'http://43.138.15.137:4000/' + </alt=""i>
            <lu>
              ulv c lass="banner-dots">
              <d iv class="dot " v-for="item, index in bannerList" :key="item.id"
                :class="{ 'dot-active': currentIndex === index }">
      </div>
    </div>
  </div>
  </div>
  <div class="quicknav">
    <ul>
      <l i>
        <img src="../../assets/imgs/home1.png" alt="">
        限时秒杀
        </li>
        <li>
          <img src="../../assets/imgs/home2.  png" alt="">
          产销商 品
          < /li>
        <li>
          <img src="../../assets/img  s/home3.png" a lt="">
          品质大牌
        </li>
        <li>
          <img src="../../as  sets/imgs/home  4.png" alt="">
          商城自营
        </li>
        <li>
          <img src="  ../../assets/i  mgs/home5.png  " alt="">
          积分商城
        </li>
    </ul>
  </div>
  <div cl ass="seckill">
    <div class="seckill-left">
      <img src="../../assets/imgs/home6.png" al t="">
    </div>
    <div class="seckill-right">
      <img src="../../assets/imgs/home7.png" alt="  ">
      <i mg src=" .. /../assets/imgs/home8.p ng" alt="">
    </div>
  </div>
  <div class="tui  jian">
    <div clas s="tuij ian-to p">
      < h4 cla ss="active">
        双十一尖货 预购
        </h4>
        <h4>
          畅购全球
        </h4>
    </div>
    <div class="tuijian-bottom">
      <ul>
        < li>
          <img s rc="ht  tp://43.138.15.137:8086/static/img/01.42fb414c.png" alt="">
          </li>
          <li>
            <img s rc="http:/ /43.138.15.137:8086/static/img/01.42fb414c.png" alt="">
          </li>
          <li>
            <img src="  http://43.138.15.137:8086/static/img/01.42fb414c.png" alt="">
          </li>
          <li>
            <img src="http://43.138.15.137:8086/static/img/01.42fb414c.png" alt="">
          </li>
          <li>
            < img src="http://43.138.15.137:  8086/static/img/01.42fb414c.png" a lt="">
          </li>
      </ul>
    </div>
  </div>
  <div class="hort">
    <div cla ss="hort-t op">
      <ul>
        <li @click="recommendIndex = 0" :class="{ 'hort-top-li-active': recommendIndex === 0 }">
          热门推 荐
        </li>
        <li @click="recommendIndex = 1" :class="{ 'hort-top-li-active': recommendIndex === 1 }">
          上新推荐
        </li>
        <li @cli ck="recommendIndex = 2" :cl ass="{  'hort -top-li- active': recommendIndex === 2 }">
          所有商品
        </li>
      </ul>
    </div>
    <div class="hort-bottom">
      <ul>
        <li v-for="item in recommendList[recommendIndex]?.content || []" :key="item.id">
          < div class="hort- box">
            <div class="hort-box-left">
              <img :src="'  http://43.138.15.137  :4000/' + item.img" alt="">
              <p>
                {{ recommendIndex === 0 ? '热门推荐' : recommendIndex === 1 ? '上新推荐 ' : '所有商品' }}
              </p>
            </div>
            <div class="hort-box-right">
              <div class="hort-box-right-top">
                <p>
                  {{ item.goodsna me }}
                </p>
                </di v>
                <div class="hort-box-right  -bottom">
                  <p>
                    {{ item.price }}元
                    <i>
                      {{ item.mar ket_pric e }}元
                    </i>
                  </p>
                  <button>立即抢购</button>
                </div>
              </div>
            </div>
        </li>
      </ul>
    </div>
  </div>
  </div>
</template>

<script>
import { getHomeAPI, getBannerAPI, getRecommendAPI } from '@/api/home/home'
export default {
  data() {
    return {
      bannerTimer: null, // 重命名timer为bannerTimer
      navList: [],
      currentIndex: 0,
      bannerList: [],
      recommendList: [],
      // 推荐商品index
      recommendIndex: 0,
      buttomNavIndex: 0,
      timer: null,
      startX// ,    // 触摸起始X坐标
      moveX: 0,     // 触摸移动X坐标
      isDragging: false // 是否正在拖拽
    }
  },
  created() {
    this.getHome()
    // 获取图片
    this.getBanner()
    // 获取推荐
    this.getRecommend()
    // this.startTimer() // 修改调用方法名称

  },
  methods: {
    async getHome() {
      let res = await getHomeAPI()
      console.log(res)
      this.navList = res.list
    },
    // 获取banner
    async getBanner() {
      let res = await getBannerAPI()
      this.bannerList = res.list
    },
    // 获取推荐
    async getRecommend() {
      let res = await getRecommendAPI()
      this.recommendList = res.list
      console.log(this.recommendList);
    },
    // 定时切换banner
    startTimer() {
      this.bannerTimer = setInterval(() => {
        this.currentIndex++
        getGoodsList(id) {
          this.$router.push({
            name: 'goodslist',
            query: {
              cateid: id,
              type: 1
            }
          })
        }, this.$refs.banner.style      // 记录触摸起始位置和时间at      this.startX = e.touches[0].clientX;        this.startTime = Date.now();Li      clearInterval(this.bannerTimer); // 清除定时器        }     handleTouchMove(e) {
        // 实时更新元素位置a       this.moveX = e.touches[0].clientX;
        const diffX = this.moveX - this.startX;
        this.$refs.banner.style.transform = `translateX(${-this.currentIndex * 10 + diffX / 10}rem)`;
        this.$refs.banner.style.transition = 'none';
      },
        handleTouchEnd() {
        // 根据滑动逻辑切换索引
        const diffX = this.moveX - this.startX;
        const diffTime = Date.now() - this.startTime;
        this.$refs.banner.style.transition = 'all 0.3s';
        // 如果滑动距离超过100px或者滑动速度很快，则切换banner
        if(Math.abs(diffX) > 100 || (Math.abs(diffX) > 20 && diffTime < 300)) {
        if (diffX > 0) {
          // 向右滑动，上一张
          this.currentIndex = this.currentIndex === 0 ? this.bannerList.length - 1 : this.currentIndex - 1;
        } else {
          // 向左滑动，下一张
          this.currentIndex = this.currentIndex === this.bannerList.length - 1 ? 0 : this.currentIndex + 1;
        }
      }
      this.$refs.banner.style.transform = `translateX(${-this.currentIndex * 10}rem)`;
      this.startTimer(); // 重新启动定时器
    }
      this.startTimer(); // 重新启动定时器
  },
  getGoodsList(id) {
    this.$router.push({
      name: 'goodslist',
      query: {
        cateid: id,
        type: 1
      }
    })
  },
},
beforeDestroy() {
  clearInterval(this.bannerTimer)
  this.bannerTimer = null
},
}

</script>